package com.sixkey.controller;


import com.sixkey.annotation.OperationLog;
import com.sixkey.entity.Department;
import com.sixkey.entity.User;
import com.sixkey.response.Response;
import com.sixkey.service.IDepartmentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2023-10-22
 */
@Slf4j
@Validated
@CrossOrigin
@RestController
@RequestMapping("/department")
@RequiredArgsConstructor
public class DepartmentController {

    private final IDepartmentService departmentService;

    /**
     * 构造数据返回
     * @return
     */
    @GetMapping("/list")
    public Response list(){
        List<Map<String,Object>> list =  departmentService.getDepartmentList();
        return Response.success(list);
    }

    /**
     * 返回部门数据给用户添加部门
     * @return
     */
    @GetMapping("/deptList")
    public Response getDeptData(){
        List<Map<String,Object>> list =  departmentService.getDeptData();
        return Response.success(list);
    }

    /**
     * 修改部门
     * @param user
     * @return
     */
    @PutMapping("/update")
    @OperationLog("修改部门")
    public Response update(@RequestBody @Valid Department department){
        log.info("修改部门请求进来");
        departmentService.updateById(department);
        return Response.success();
    }

    /**
     * 部门数据回显
     * @param
     * @return
     */
    @GetMapping("/echo/{id}")
    public Response getUserInfo(@PathVariable("id") Integer id){
        log.info("部门数据回显请求进来");
        Department department = departmentService.getById(id);
        return Response.success(department);
    }

    /**
     * 新增部门
     * @param department
     * @return
     */
    @OperationLog(value = "新增部门")
    @PostMapping("/save")
    public Response save(@RequestBody @Valid Department department) {
        departmentService.save(department);
        return Response.success();
    }

    /**
     * 删除部门
     * @param
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @OperationLog("删除部门")
    public Response remove(@PathVariable("id") Integer id){
        log.info("删除部门请求进来");
        departmentService.removeDept(id);
        return Response.success();
    }
}
